package com.xuecheng.orders.api;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.internal.util.AlipaySignature;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.google.common.collect.Maps;
import com.xuecheng.base.exception.StudySystemException;
import com.xuecheng.orders.config.AlipayConfig;
import com.xuecheng.orders.model.dto.AddOrderDto;
import com.xuecheng.orders.model.dto.PayRecordDto;
import com.xuecheng.orders.model.dto.PayStatusDto;
import com.xuecheng.orders.model.po.XcPayRecord;
import com.xuecheng.orders.service.OrderService;
import com.xuecheng.orders.util.SecurityUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Iterator;
import java.util.Map;
import java.util.Objects;

@Api(value = "订单支付接口", tags = "订单支付接口")
@Slf4j
@Controller
public class OrderController {
    @Value("${pay.alipay.APP_ID}")
    String APP_ID;
    @Value("${pay.alipay.APP_PRIVATE_KEY}")
    String APP_PRIVATE_KEY;

    @Value("${pay.alipay.ALIPAY_PUBLIC_KEY}")
    String ALIPAY_PUBLIC_KEY;
    @Autowired
    OrderService orderService;
    /** 已支付 **/
    private final static String PAID = "601002";
    private final static String ORDER_STRING_TEMPLATE = "{ \"out_trade_no\":\"%s\", \"total_amount\":\"%s\"," +
            " \"subject\":\"%s\", \"product_code\":\"%s\" }";

    @ApiOperation("生成支付二维码")
    @PostMapping("/generatepaycode")
    @ResponseBody
    public PayRecordDto generatePayCode(@RequestBody AddOrderDto addOrderDto) {
        SecurityUtil.XcUser user = SecurityUtil.getUser();
        if (Objects.isNull(user)) {
            StudySystemException.cast("未登录");
        }
        return orderService.createOrder(user.getId(), addOrderDto);
    }

    @ApiOperation("扫码下单接口")
    @GetMapping("/requestpay")
    public void requestpay(String payNo,HttpServletResponse httpResponse) throws IOException {
        //如果payNo不存在则提示重新发起支付
        XcPayRecord payRecord = orderService.getPayRecordByPayno(payNo);
        if (payRecord == null) {
            StudySystemException.cast("请重新点击支付获取二维码");
        }
        //支付状态
        String status = payRecord.getStatus();
        if (PAID.equals(status)) {
            StudySystemException.cast("订单已支付，请勿重复支付。");
        }
        //构造sdk的客户端对象
        AlipayClient client = new DefaultAlipayClient(AlipayConfig.URL,
                APP_ID, APP_PRIVATE_KEY,
                AlipayConfig.FORMAT,
                AlipayConfig.CHARSET,
                ALIPAY_PUBLIC_KEY,
                AlipayConfig.SIGNTYPE);//获得初始化的AlipayClient
        AlipayTradeWapPayRequest alipayRequest = new AlipayTradeWapPayRequest();//创建API对应的request
        String bizContent = String.format(ORDER_STRING_TEMPLATE,
                payRecord.getPayNo(),
                payRecord.getTotalPrice(),
                payRecord.getOrderName(),
                "QUICK_WAP_PAY");
        alipayRequest.setNotifyUrl("http://tjxt-user-t.itheima.net/orders/paynotify");
        alipayRequest.setBizContent(bizContent);//填充业务参数
        String form = "";
        try {
            //请求支付宝下单接口,发起http请求
            form = client.pageExecute(alipayRequest).getBody(); //调用SDK生成表单
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        httpResponse.setContentType("text/html;charset=" + AlipayConfig.CHARSET);
        httpResponse.getWriter().write(form);//直接将完整的表单html输出到页面
        httpResponse.getWriter().flush();
        httpResponse.getWriter().close();
    }

    @ApiOperation("查询支付结果")
    @GetMapping("/payresult")
    @ResponseBody
    public PayRecordDto payresult(String payNo) throws IOException {
        //调用支付宝接口查询
        return orderService.queryPayResult(payNo);
    }

    @PostMapping("/paynotify")
    public void paynotify(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Map<String, String> params = Maps.newHashMap();
        Map<String, String[]> parameterMap = request.getParameterMap();
        Iterator<String> iterator = parameterMap.keySet().iterator();
        while (iterator.hasNext()) {
            String name = iterator.next();
            String[] values = parameterMap.get(name);
            String str = "";
            for (int i = 0; i < values.length; i++) {
                str = (i == values.length - 1) ? str + values[i] : str + values[i] + ",";
            }
            params.put(name, str);
        }
        boolean verifyResult = AlipaySignature.rsaCheckV1(params, ALIPAY_PUBLIC_KEY, AlipayConfig.CHARSET, "RSA2");
        if (verifyResult) {
            String outTradeNo = new String(request.getParameter("out_trade_no")
                    .getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
            String tradeNo = new String(request.getParameter("trade_no").getBytes(StandardCharsets.ISO_8859_1),
                    StandardCharsets.UTF_8);
            String tradeStatus = new String(request.getParameter("trade_status")
                    .getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
            String totalAmount = new String(request.getParameter("total_amount")
                    .getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
            if ("TRADE_SUCCESS".equals(tradeStatus)) {
                // 更新支付记录表的支付状态以及订单表的状态
                PayStatusDto dto = new PayStatusDto();
                dto.setTrade_status(tradeStatus);
                dto.setApp_id(APP_ID);
                dto.setOut_trade_no(outTradeNo);
                dto.setTrade_no(tradeNo);
                dto.setTotal_amount(totalAmount);
                orderService.saveAliPayStatus(dto);
            }
            response.getWriter().write("success");
        }
    }
}